package com.game.mongo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Set;

import static org.springframework.data.mongodb.core.query.Criteria.where;
import static org.springframework.data.mongodb.core.query.Query.query;

/**
 *
 * 只提供了基本的方法，如有需要，可以去继承此类，自己实现
 * Created by legend9999 on 2016/6/5.
 */
@Component
public class UserMongo {
	private Logger logger = LoggerFactory.getLogger(UserMongo.class);
	public static int startUserId = 100000;
	@Autowired
	private MongoTemplate mongoTemplate;

	public User findByUserId(int userId) {
		Query query = new Query();
		query.addCriteria(Criteria.where(UserField.userId).is(userId));
		return mongoTemplate.findOne(query, User.class);
	}

	public void addUser(User user) {
		if (user.getUserId() == 0) {
			logger.error("请设置UserId");
			return;
		}
		mongoTemplate.insert(user);
	}

	public User findByLoginName(String loginName) {
		return mongoTemplate.findOne(query(where(UserField.loginName).is(loginName)), User.class);
	}

	public void incField(int userId, String key, long addValue) {
		Query query = new Query();
		query.addCriteria(Criteria.where(UserField.userId).is(userId));
		Update update = new Update();
		update.inc(key, addValue);
		mongoTemplate.updateFirst(query, update, User.class);
	}

	public void setField(int userId, String key, long value) {
		if (key.equals(UserField.yuanbao) || key.equals(UserField.yuanbao)) {
			logger.error("UserMongo.setField方法中，不允许出现coin,yuanbao字段！！！！！！");
			return;
		}
		Query query = new Query();
		query.addCriteria(Criteria.where(UserField.userId).is(userId));
		Update update = new Update();
		update.set(key, value);
		mongoTemplate.updateFirst(query, update, User.class);
	}

	public void setField(int userId, Map<String, Object> fieldMap) {
		if (fieldMap.containsKey(UserField.yuanbao) || fieldMap.containsKey(UserField.yuanbao)) {
			logger.error("UserMongo.setField方法中，不允许出现coin,yuanbao字段！！！！！！");
			return;
		}
		Query query = new Query();
		query.addCriteria(Criteria.where(UserField.userId).is(userId));
		Update update = new Update();
		Set<Map.Entry<String, Object>> entries = fieldMap.entrySet();
		for (Map.Entry<String, Object> entry : entries) {
			update.set(entry.getKey(), entry.getValue());
		}
		mongoTemplate.updateFirst(query, update, User.class);
	}

	public void incField(int userId, Map<String, Long> fieldMap) {
		Query query = new Query();
		query.addCriteria(Criteria.where(UserField.userId).is(userId));
		Update update = new Update();
		Set<Map.Entry<String, Long>> entries = fieldMap.entrySet();
		for (Map.Entry<String, Long> entry : entries) {
			update.inc(entry.getKey(), entry.getValue());
		}
		mongoTemplate.updateFirst(query, update, User.class);
	}

	public void incCoin(int userId, long addCoin) {
		incField(userId, UserField.coin, addCoin);
	}

	public void incYuanbao(int userId, long addCoin) {
		incField(userId, UserField.yuanbao, addCoin);
	}

	public int getNextUserId() {
		Query query = new Query();
		query.addCriteria(Criteria.where(UserId.UserIdField.name).is(UserId.UserIdField.userId));

		Update update = new Update();
		update.set(UserId.UserIdField.name, UserId.UserIdField.userId);
		update.inc(UserId.UserIdField.userId, 1);

		UserId userId = mongoTemplate.findAndModify(
				query, update,
				new FindAndModifyOptions().returnNew(true), UserId.class);
		if (userId == null) {
			userId = new UserId();
			userId.setUserId(startUserId);
			userId.setName(UserId.UserIdField.userId);
			mongoTemplate.insert(userId);
		}
		return userId.getUserId();
	}
}
